package com.szjz.rabbitmq.client.topic;

import com.rabbitmq.client.*;
import com.szjz.rabbitmq.client.util.RabbitMQUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 史博
 * @desc 动态路由
 * @date 2020/8/25 5:05 下午 周二
 */

@Slf4j
public class Consumer1 {
    @SneakyThrows
    public static void main(String[] args) {
        // 创建连接和通道
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        // 声明交换机
        String exchangeName = "topics";
        channel.exchangeDeclare(exchangeName, "topic");
        // 声明临时队列
        String queueName = channel.queueDeclare().getQueue();
        // 将交换机个队列绑定
        channel.queueBind(queueName, exchangeName, "user.*");
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) {
                log.info("topic 消费者--01: {}", new String(body));
            }
        });

    }

}
